package io.mya.modules.api.webinterfz.utils;

import org.apache.commons.codec.digest.DigestUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/10/23.
 */
public class SignUtils {
    /**
     * 请求数据验签
     *
     * @param map
     * @param key
     * @return
     */
    public static boolean sing(Map<String, Object> map, String keys) {
        String sign = String.valueOf(map.get("sign"));
        map.remove("sign");
        List<String> keyList = new ArrayList<String>();
        keyList.addAll(map.keySet());
        StringBuffer signStr = new StringBuffer();
        for (String key : keyList) {
            if (null != map.get(key) && !"".equals(map.get(key))) {
                signStr.append(key).append("=").append(map.get(key)).append("&");
            }
        }
        signStr.delete(signStr.length() - 1, signStr.length()).append(keys);
        return sign.equals(DigestUtils.md5Hex(signStr.toString()));
    }

}
